//注册部分

//获取验证码
$('.smscode').bind('tap', function() {
	console.log($('.smscode').attr("disabled"))
	if($('.smscode').attr("disabled") == 'disabled') {
		mui.toast("请稍后再试")
		return
	}
	if($("#account").val() == "") {
		mui.toast("手机号不能为空")
		$('#smscode').blur()
		$('#account').focus()
		return;
	}
	if(!isPoneAvailable($("#account").val())) {
		mui.toast("请输入正确的手机号")
		return
	}
	var rndTime = new Date().getTime();
	var str = $("#account").val();
	var codeNum = str.substring(5);
	var validStr = hex_md5(
		codeNum +
		hex_md5(str) +
		rndTime +
		"352c51da88c653c83326941fca5f83b5"
	);
	var parms = {
		phone: $("#account").val(),
		validStr: validStr,
		rndTime: rndTime
	}
	postServer('/app/sms/sendSms', $.param(parms), function(res) {
		console.log(JSON.stringify(res))
		//				console.log(res.data.code)
		if(res.success) {
			mui.toast("请注意查收短信")
			invokeSettime('.smscode')
		} else {
			mui.toast(res.message)
		}
	});
})
var regflag = true;
$('.agreement').bind('tap', function() {
	regflag = true
	var gvtable = document.getElementsByName("checkbox1");
	for(var i = 0; i < gvtable.length; i++) {
		if(!gvtable[i].checked) {
			mui.toast('请同意协议')
			regflag = false;
			break
		}
	}
	if(regflag) {
		$('.alert').hide()
	}
})
$('#password').bind('blur',function(){
	var reg = /^[a-zA-Z0-9!@#$%?><.,;:|]{6,18}$/
	if(!reg.test($('#password').val())){
		mui.toast("密码不符合要求，请重新输入")
		return;
	}
})
//注册
$('.reg').bind("tap", function() {
	if($("#account").val() == "") {
		mui.toast("手机号不能为空")
		return;
	}
	//校验重复密码是否一致
	if($("#password").val() == "") {
		mui.toast("密码为空")
		return;
	}
	if($("#password").val() != $("#repassword").val()) {
		mui.toast("两次密码不一致")
		return;
	}
	if($("#smscode").val() == "") {
		mui.toast("验证码为空")
		return;
	}
	var parms = {
		phone: $("#account").val(),
		password: $("#password").val(),
		rePassword: $("#repassword").val(),
		smsCode: $("#smscode").val(),
		recommendCode: "",
		openId: "",
		device: ""
	}
	postServer('/app/reg', $.param(parms), function(res) {
		console.log(JSON.stringify(res))
		if(res.success) {
			mui.toast("注册成功")
			login()
			//			goHome();
		} else {
			mui.toast(res.message);
			return
		}
	}, function(res) {
		console.log(JSON.stringify(res))
		mui.toast(res.message);
		mui.toast(res.message)
	});
})

$('.homebtn').bind("tap", function() {
	goHome()
})

function login() {
	var subData = {
		phone: $("#account").val(),
		password: $("#password").val(),
		openId: ""
	}

	postServer('/app/member/login', $.param(subData), function(res) {
		if(res.success) {
			localStorage.setItem('phone', $("#account").val())
			localStorage.setItem('userinfo', JSON.stringify(res.data))
			plus.webview.getLaunchWebview().evalJS("activeModel()")
			plus.webview.getWebviewById('../landing/login.html').close()
			plus.webview.currentWebview().close()
		} else {
			mui.toast(res.message)
		}
	}, function(res) {
		localStorage.setItem("password", "")
		localStorage.setItem("isManual", "false")
		mui.toast(res.message)
	});
}

function goHome() {
	plus.webview.getWebviewById('../landing/login.html').close()
	plus.webview.currentWebview().close()
}